package graphic;

import Serializable.ParsedCmd;
import editor.Eloop;
import input.Input;
import input.InputBox;
import java.util.logging.Level;
import java.util.logging.Logger;
import klient.Bridge;
import klient.Connection;
import klient.Klient;
import klient.Sender;
import klient.UserState;
import objects.LoadingAnim;
import objects.Text2dPrinter;
import openGL11.Engine;
import org.lwjgl.opengl.Display;

/**
 *  
 * @author Marcin & Bartek
 * 
 * @description Main menu loop
 */
public class MainMenuLoop { 
    public String errLabel="";
    public boolean buttonsEnabled=true;
    public boolean needList=true;
    private int serv=0;
    private int toProcess=0;
    public LoadingAnim lA;
    private String opt[][] = new String[][]{
        {"Demo","Multiplayer","Edytor","Wyjscie"},//0
        {"Hostuj","Dołącz","O autorach","Wyjscie"},//1
        {"_Login"," ","_Haslo"," ","OK","Wstecz"},//2
        {"_Login"," ","_Haslo"," ","OK","Wstecz"},//3
        {"_Barłomiej Płotka","_Marcin Piątkowski","Wstecz"},//4
        {"_IP"," ","_Port"," ","OK","Wstecz"},//5
        {"Zaloguj","Zarejestruj","Wstecz"},//6
        {},//7, reserved
        {"_Nazwa serwera","","Dla dwoch graczy","Dla czterech rzeczy","Wstecz"},//8
        };
    
    private void processing(){
        if(toProcess!=0)
            Bridge.setButtonsEnabled(false);  
        switch(toProcess){
            case 0:
                return;
            case 1://connect
                if(Klient.uS.getState()!=UserState.WAIT_FOR_CONNECT)
                    afterProcess();
                break;
            case 2://logIn
                if(!Bridge.opPending)
                    afterProcess();
                break;
            case 3://create
                if(!Bridge.opPending)
                    afterProcess();
                break;
            case 4:
                if(!Bridge.opPending)
                    afterProcess();
                break;
        }
        
    }
    private void afterProcess(){
        Bridge.setButtonsEnabled(true);
        switch(toProcess){
            case 0:
                return;
            case 1:
                
                if(Klient.uS.getState()>=UserState.JUST_CONNECTED)
                    menuNr=6;
                else
                    Bridge.setErrLabel("Brak polaczenia!");
                toProcess=0;
                return;
            case 2:
                if(Bridge.succes){
                    MenuMulti mm = new MenuMulti();
                    mm.loop();
                    //tutaj trafimy po wyjściu, więc
                menuNr=1;
                needList=true;
                }else
                    Bridge.setErrLabel("Nie mozna polaczyc");
                toProcess=0;
                break;
            case 3:
                logIn(logIB.txt);
                break;
            case 4:
                toProcess=0;
                break;
        }
        
    }
            
    
    
    private void connect(){
        if(Klient.uS.getState() >UserState.NO_CONNECTED){
            menuNr=6;
        }else{
            Klient.uS.setState(UserState.WAIT_FOR_CONNECT);
            Bridge.setErrLabel("Laczenie...");
            Bridge.setButtonsEnabled(false);
            
            if(!logIB.txt.isEmpty()) Connection.HOST=logIB.txt;
            else Connection.HOST="localhost";
            
            if(!regIB.txt.isEmpty()) Connection.SPORT=Integer.parseInt(regIB.txt);
            else Connection.SPORT=8870;
            
            Klient.con=new Sender();
            Klient.con.execute();
            
            toProcess=1;            
        }
    }
    
    private void Register() {                          
        if(Klient.uS.loginState != UserState.HAS_ID){
            Bridge.setErrLabel("Serwer nieosiagalny!");
            return;
        }
        if(!logIB.txt.isEmpty()&& !(regIB.txt.isEmpty())){
            Bridge.setErrLabel("Rejestracja - prosze czekac...");
            Bridge.setOpPending(true);
            Klient.uS.wantReg=true;
            Klient.uS.nick=logIB.txt;
            Klient.uS.pass=Connection.hash(regIB.txt.toCharArray());
            regIB.txt="";
            
            toProcess=4;
        }
        else
            Bridge.setErrLabel("Wpisz haslo i login");
    }                         

    private void Login() {              
        if(Klient.uS.loginState != UserState.HAS_ID){
            Bridge.setErrLabel("Serwer nieosiagalny!");
            return;
        }
        if(!logIB.txt.isEmpty()&& !(regIB.txt.isEmpty())){
            Bridge.setOpPending(true);
            Bridge.setErrLabel("Logowanie - prosze czekac...");
            Klient.uS.wantReg=false;
            Klient.uS.nick=logIB.txt;
            Klient.uS.pass=Connection.hash(regIB.txt.toCharArray());
            regIB.txt="";   
            toProcess=4;
        }
        else
            Bridge.setErrLabel("Wpisz haslo i login");
    } 
    private void logIn(String s){
        Bridge.setOpPending(true);
        Bridge.setCmd(new ParsedCmd("JOIN",s));
        Bridge.setNeedAny(true);
        toProcess=2;
    }
    private void create(int nr,String name){
        Bridge.setOpPending(true);
        Bridge.setCmd(new ParsedCmd("NGAME",nr));
        Bridge.cmd.stringParam=new String[]{name};
        Bridge.setNeedAny(true);
        toProcess=3;
    }
    private void getSlist(){
        Bridge.setOpPending(true);
        Bridge.setCmd(new ParsedCmd("GETSLIST"));
        Bridge.setNeedAny(true);
        
        while(Bridge.opPending){
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(MainMenuLoop.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        serv=Bridge.ret.intParam[0];
        opt[7] = new String[Bridge.ret.intParam[0]+2];
        for(int i=0;i<Bridge.ret.intParam[0];++i)
            opt[7][i]=Bridge.ret.stringParam[i];
        
        opt[7][serv]="Odswiez";
        opt[7][serv+1]="Wstecz";
        needList = false;
    }
    
    public int menuNr=0;
    
    InputBox logIB=new InputBox(20,150,180,210);    
    InputBox regIB=new InputBox(20,150,240,270);
    
    public void choose(int menu,int nr){
        regIB.act=false;
        logIB.act=false;
        if(menu==0){
            switch(nr){
                case 0://Demo
                    new Loop(Graphic.e);
                    break;
                case 1://Multi
                    if(Klient.uS.loginState>=UserState.JUST_CONNECTED){
                        if(Klient.uS.loginState>=UserState.IS_PASS){
                            menuNr=1;
                        }else{
                            menuNr=6;
                        }                            
                    }else
                        menuNr=5;
                    break;
                case 2:
                    new Eloop(Graphic.e);
                    break;
                case 3://Wyjście
                    System.exit(0);
                    break;
            }
        }
        if(menu==1){
            switch(nr){
                case 0://hostuj
                    menuNr=8;
                    break;
                case 1://dołącz
                    menuNr=7;
                    break;
                case 2://o autorach
                    menuNr=4;
                    break;
                case 3://Wyjście
                    System.exit(0);
                    break;
            }
        }
        if(menu==4){
            switch(nr){
                case 2://o autorach
                    menuNr=1;
                    break;
            }
        }
        if(menu==2){
            switch(nr){
                case 1://login
                    logIB.act=true;
                    break;
                case 3://rejestracja
                    regIB.act=true;
                    break;
                case 4://Ok
                    Klient.log("Start logowania!");
                    Login();
                    break;
                case 5://Wstecz
                    menuNr=0;
            }
        }
        if(menu==3){
            switch(nr){
                case 1://login
                    logIB.act=true;
                    break;
                case 3://rejestracja
                    regIB.act=true;
                    break;
                case 4://Ok
                    Register();
                    break;
                case 5://Wstecz
                    menuNr=0;
            }
        }
        if(menu==5){
            switch(nr){
                case 1://login
                    logIB.act=true;
                    break;
                case 3://rejestracja
                    regIB.act=true;
                    break;
                case 4://Ok
                    if(Klient.uS.getState()>UserState.NO_CONNECTED)
                        menuNr=6;
                    else
                        connect();
                    break;
                case 5://Wstecz
                    menuNr=0;
            }
            
        }
        if(menu==6){
            switch(nr){
                case 0://login
                    menuNr=2;
                    break;
                case 1://rejestracja
                    menuNr=3;
                    break;
                case 2:
                    menuNr=0;
            }
            
        }
        if(menu==7){
            if(nr==serv){
                needList=true;
            }
            else if(nr==serv+1){
                menuNr=1;
            }else{
                logIn(opt[7][nr]);
            }
            
        }
        if(menu==8){
            switch(nr){
                case 1://login
                    logIB.act=true;
                    break;
                case 2:
                    create(2,logIB.txt);
                    break;
                case 3://rejestracja
                    create(4,logIB.txt);
                    break;
                case 4:
                    menuNr=1;
            }
            
        }
        if(!logIB.act && !regIB.act)
            logIB.act=true;
    }
    
    public MainMenuLoop(Engine e){
        e.start2D();
        e.setClearColor(new Color(0,0,0,1));
               
        lA=new LoadingAnim(10,Graphic.e.getWidth()/2,e.getHeight()/2,100);
        regIB.pass=true;
        Text2dPrinter wrBig = new Text2dPrinter();        
        wrBig.setFont("Alan Den.ttf",40);
        wrBig.setColor(org.newdawn.slick.Color.white);
        
        Text2dPrinter wr = new Text2dPrinter();        
        wr.setFont("Alan Den.ttf",20);
        wr.setColor(org.newdawn.slick.Color.white);
        Input.init();
        int pMenu=-1;
        while(true){
            
            if(pMenu!=menuNr){
                logIB.txt="";
                regIB.txt="";
                Bridge.errLabel="";
                pMenu=menuNr;
            }
            errLabel=Bridge.errLabel;
            buttonsEnabled=Bridge.buttonsEnabled;
            if(errLabel.equals("OK")){
                Bridge.setErrLabel("");
                menuNr=1;
            }
            e.clear();
            Input.refresh();
            if(menuNr==2||menuNr==3||menuNr==5){
                if(menuNr==5) regIB.pass=false;
                else regIB.pass=true;
                logIB.refresh();
                regIB.refresh();
            }else if(menuNr==1){
                wr.setColor(org.newdawn.slick.Color.white);
                wr.write(20, 80, "Witaj, "+Klient.uS.nick+"!");
            }else if(menuNr==8){
                logIB.refresh();
            }
            wrBig.write(e.getWidth()/2-150, 10, "MultiDefence 3D");
            if(buttonsEnabled && Input.LMBdown() && Input.x()>20 && Input.x()<150){
                int y=Input.y();
                y-=150;
                if(y>0){
                    y/=30;
                    if(y<opt[menuNr].length) choose(menuNr,y);
                }
            }
            
            if(menuNr==7 && needList){
                getSlist();
                
            }
            for(int i=0;i<opt[menuNr].length;++i){
                if(Input.mouseIn(20,150,i*30+150,i*30+180) && !opt[menuNr][i].startsWith("_")){
                    wr.setColor(org.newdawn.slick.Color.red);
                }else wr.setColor(org.newdawn.slick.Color.white);
                wr.write(20, i*30+150, opt[menuNr][i]);
            }
            
            if(!errLabel.isEmpty()){
                wr.setColor(org.newdawn.slick.Color.red);
                wr.write(20, 500, errLabel);
            }
            processing();
            if(toProcess!=0)            
                lA.draw();
            Display.update();  
            Display.sync(30);
        }
    }
}
